import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { getShopInfoAPI, type ShopItem } from "../../api/shops";




export interface shopsState {
    Shops: ShopItem[] | [];
    skip: number; //跳过几个
    loading: boolean; // 加载状态
    error: string | null; // 错误信息
}

const initialState: shopsState = {
    Shops: [],
    skip: 0,
    loading: false,
    error: null
}
const shopsSclice = createSlice({
    name: 'shops',
    initialState,
    reducers: {

    }, extraReducers: (builder) => {
        builder
            .addCase(getShopsInfo.pending, (state) => {
                state.loading = true;
                state.error = null
            })
            .addCase(getShopsInfo.fulfilled, (state, action) => {
                state.loading = false;
                state.Shops = action.payload.data
            })
            .addCase(getShopsInfo.rejected, (state, action) => {
                state.loading = false;
                state.error = action.error.message || "获取商家信息失败";
            });
    },
})
// export const { setShops } = shopsSclice.actions
export const shopsReducer = shopsSclice.reducer

export const getShopsInfo = createAsyncThunk("shops/getShopsInfo", async () => {
    try {
        const res = await getShopInfoAPI()
        console.log(res.data)
        return res.data
    } catch (error) {
        console.log(error)
        const res ={
            data:[]
        }
        return res
    }

})